package com.vrp3d.service.impl;

import com.vrp3d.common.context.InvokerContext;
import com.vrp3d.common.enums.CommonEnum;
import com.vrp3d.common.enums.LanguageTypeEnum;
import com.vrp3d.common.enums.UserExceptionEnum;
import com.vrp3d.common.exceptions.UserException;
import com.vrp3d.common.utils.DateUtil;
import com.vrp3d.common.utils.ObjUtil;
import com.vrp3d.domain.dto.showroom.ShowroomStatusPassDTO;
import com.vrp3d.domain.model.UserContext;
import com.vrp3d.service.AbsService;
import com.vrp3d.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 通用接口实现
 *
 * @author vrp3d
 */
@Slf4j
@Service
public abstract class AbsServiceImpl implements AbsService {

    @Autowired
    private RedisService redisService;

    @Override
    public UserContext getUser() {
        return InvokerContext.getUserCtx();
    }

    @Override
    public Date getNowTime() {
        return new Date();
    }

    @Override
    public Integer getNormalStatus() {
        return CommonEnum.STATUS_NORMAL.getMsgInteger();
    }

    @Override
    public Integer getDeletedStatus() {
        return CommonEnum.STATUS_DELETED.getMsgInteger();
    }

    @Override
    public String getFileStoragePath() {
        return getUser().getId() + DateUtil.getFileStoragePath();
    }

    @Override
    public String getDeleteStoragePath() {
        return CommonEnum.FOLDER_BACK_DELETED.getMsg() + getUser().getId();
    }

    @Override
    public void verifyPhoneVerificationCodeCanItBeSent(String mobile, CommonEnum oneMinuteOneMessage, CommonEnum oneHourFiveMessage, CommonEnum oneDayTenMessage) {
        // 验证1条/min
        if (StringUtils.isNotBlank(redisService.query(oneMinuteOneMessage.getMsg() + mobile))) {
            throw new UserException(UserExceptionEnum.LOGIN_ONE_MINUTE_ONE_MESSAGE);
        }
        // 验证5条/hour
        String loginOneHourFiveMessage = redisService.query(oneHourFiveMessage.getMsg() + mobile);
        if (StringUtils.isNotBlank(loginOneHourFiveMessage)) {
            int i = Integer.parseInt(loginOneHourFiveMessage);
            if (i >= oneHourFiveMessage.getMsgInteger()) {
                throw new UserException(UserExceptionEnum.LOGIN_ONE_HOUR_FIVE_MESSAGE);
            }
        }
        // 验证10条/day
        String loginOneDayTenMessage = redisService.query(oneDayTenMessage.getMsg() + mobile);
        if (StringUtils.isNotBlank(loginOneDayTenMessage)) {
            int i = Integer.parseInt(loginOneDayTenMessage);
            if (i >= oneDayTenMessage.getMsgInteger()) {
                throw new UserException(UserExceptionEnum.LOGIN_ONE_DAY_TEN_MESSAGE);
            }
        }
    }

    @Override
    public void recordAfterSendingVerificationCode(String mobile, CommonEnum oneMinuteOneMessage, CommonEnum oneHourFiveMessage, CommonEnum oneDayTenMessage) {
        // 发送后记录1条/min
        redisService.putCacheWithExpireTime(oneMinuteOneMessage.getMsg() + mobile, oneMinuteOneMessage.getMsg(), DateUtil.SECONDS_PER_MINUTE);
        // 5条/hour
        String loginOneHourFiveMessage = redisService.query(oneHourFiveMessage.getMsg() + mobile);
        if (StringUtils.isNotBlank(loginOneHourFiveMessage)) {
            redisService.incrByKey(oneHourFiveMessage.getMsg() + mobile, 1);
        } else {
            redisService.putCacheWithExpireTime(oneHourFiveMessage.getMsg() + mobile, "1", DateUtil.SECONDS_PER_HOUR);
        }
        // 10条/day
        String loginOneDayTenMessage = redisService.query(oneDayTenMessage.getMsg() + mobile);
        if (StringUtils.isNotBlank(loginOneDayTenMessage)) {
            redisService.incrByKey(oneDayTenMessage.getMsg() + mobile, 1);
        } else {
            redisService.putCacheWithExpireTime(oneDayTenMessage.getMsg() + mobile, "1", DateUtil.howManySecondsUntilMidnight().intValue());
        }
    }

    @Override
    public String chooseLanguage() {
        if (ObjUtil.isNotEmpty(getUser())) {
            UserContext user = getUser();
            Integer userId = user.getId();
            String redisUserLanguage = redisService.query(CommonEnum.REDIS_USER_LANGUAGE.getMsg() + userId);
            if (StringUtils.isNotBlank(redisUserLanguage)) {
                return redisUserLanguage;
            }
        }
        return LanguageTypeEnum.zh.getMsg();
    }
}
